package com.unknowpupil.common.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.util.List;

/**
 * @Classname: GsonUtils
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2020/1/4 13:04
 * @Description:
 * @Version: 1.0
 */
public class GsonUtils<T> {
    /**
     * 功能描述: <br>
     * 〈转换成json格式〉
     *
     * @param: obj
     * @since: 1.0.0
     * @auther: w10g
     * @return:
     * @Date: 2020/1/4 13:14
     */
    public static String toJson(Object obj) {
        Gson gson = new GsonBuilder()
                .setPrettyPrinting()
                .create();
        return gson.toJson(obj);
    }
/**
 * 功能描述: <br>
 * 〈json数组转换成list对象〉
 *
 * @param: items json数组
 * @since: 1.0.0
 * @auther: w10g
 * @return:
 * @Date: 2020/1/4 13:14
 */
    public static<T> List<T> JsonItem2ListObject(String items){
        Gson gson = new Gson();
        List<T> result = gson.fromJson(items,new TypeToken<List<T>>(){}.getType());
        return result;
    }
}
